﻿using System;
using System.IO;

namespace WebCoreAdmin.Common
{
    public class ImageTypeCheck
    {
        public static ImageType Check(string filepath )
        {
            try
            {
                FileStream fs = new FileStream(filepath, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
                byte[] sb = new byte[2];
                fs.Read(sb, 0, sb.Length);
                fs.Close();
                fs.Dispose();
                string strFlag = sb[0] + sb[1].ToString();
                switch (strFlag)
                {
                    //JPG格式
                    case "255216":
                        return ImageType.Jpg;
                    //GIF格式
                    case "7173":
                        return ImageType.Gif;
                    //BMP格式
                    case "6677":
                        return ImageType.Bmp;
                    //PNG格式
                    case "13780":
                        return ImageType.Png;
                    //其他格式
                    default:
                        return ImageType.UnKnow;
                }
            }
            catch (Exception)
            {
                return ImageType.UnKnow;
            }
         
        }
    }

    public enum ImageType
    {
        Jpg,Gif,Bmp,Png,UnKnow
    }
}